package com.cspg.design;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.cspg.design.utils.AppManager;
import com.cspg.design.widget.CSPGProgressBar;
import com.cspg.design.widget.IView;

import java.util.ArrayList;

public class CheckPermissionsActivity extends AppCompatActivity implements IView {

    private CSPGProgressBar cspgProgressBar;
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestPermission();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        closeActivity(this);
    }
    public void openNewActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    public void openNewActivityByIntent(Class<?> cls, Intent intent) {
        if (null == intent) {
            return;
        }
        intent.setClass(this, cls);
        startActivity(intent);
    }

    public void openNewActivity(Class<?> cls) {
        openNewActivity(cls, null);
    }

    public void closeActivity() {
        AppManager.getAppManager().finishActivity();
    }

    public void closeActivity(Activity activity) {
        AppManager.getAppManager().finishActivity(activity);
    }

    @Override
    public void showProgress() {
        if (cspgProgressBar == null) {
            cspgProgressBar = CSPGProgressBar.show(this);
        } else {
            cspgProgressBar.show();
        }
    }

    @Override
    public void hideProgress() {
        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
    }

    @Override
    public <T> void httpSuccessRender(T t, int order) {

    }

    @Override
    public <T> void httpFailureRender(T t, int order) {

    }

    @Override
    public void uploadSuccess(String filePath) {

    }

    @Override
    public void uploadFailure(String msg) {

    }

    /**
     * Android6.0之后需要动态申请权限
     */
    private void requestPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            ArrayList<String> permissionsList = new ArrayList<>();
            String[] permissions = {
                    Manifest.permission.ACCESS_NETWORK_STATE,
                    Manifest.permission.INTERNET,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_WIFI_STATE,
                    Manifest.permission.WRITE_SETTINGS,
            };

            for (String perm : permissions) {
                if (PackageManager.PERMISSION_GRANTED != checkSelfPermission(perm)) {
                    permissionsList.add(perm);
                    // 进入到这里代表没有权限.
                }
            }

            if (!permissionsList.isEmpty()) {
                String[] strings = new String[permissionsList.size()];
                requestPermissions(permissionsList.toArray(strings), 0);
            }
        }
    }
}
